package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "分类管理")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "新增分类")
    @PostMapping()
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        Category category = Category.builder()
                .id(categoryDTO.getId())
                .type(categoryDTO.getType())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .build();
        categoryService.save(category);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询分类")
    public Result<PageResult> queryPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询分类：{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.queryPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result delete(@RequestParam Long id){
        log.info("要删除的员工id为：{}",id);
        categoryService.delete(id);
        return Result.success();
    }

    @ApiOperation("启用、禁用分类")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){

        log.info("将要启用或禁用分类:id={},status={}",id,status);
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        categoryService.startOrStop(category);
        return Result.success();
    }

    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类:{}",categoryDTO);
        Category category = Category.builder()
                .id(categoryDTO.getId())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .type(categoryDTO.getType())
                .build();
        categoryService.update(category);
        return Result.success();
    }

    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> queryByType(Integer type){
        log.info("根据类型查询分类：{}",type);
        Category category = Category.builder()
                .type(type)
                .status(StatusConstant.ENABLE)
                .build();
        List<Category> list = categoryService.queryByType(category);
        return Result.success(list);
    }
}
